/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.bis.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.bis.entity.BisDispatch;
import com.jeesite.modules.bis.service.BisDispatchService;
import com.jeesite.modules.sys.entity.Area;
import com.jeesite.modules.sys.service.AreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 派单管理Controller
 *
 * @author Zhangning
 * @version 2019-08-24
 */
@Controller
@RequestMapping(value = "${adminPath}/bis/bisDispatch")
public class BisDispatchController extends BaseController {

    @Autowired
    private BisDispatchService bisDispatchService;

    @Autowired
    private AreaService areaService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public BisDispatch get(String id, boolean isNewRecord) {
        return bisDispatchService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("bis:bisDispatch:view")
    @RequestMapping(value = {"list", ""})
    public String list(BisDispatch bisDispatch, Model model) {
        model.addAttribute("bisDispatch", bisDispatch);
        return "modules/bis/bisDispatchList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("bis:bisDispatch:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<BisDispatch> listData(BisDispatch bisDispatch, HttpServletRequest request, HttpServletResponse response) {
        int overtime = Integer.parseInt(Global.getConfig("bis.dispatch.overtime.minutes", "10"));
        bisDispatch.setPage(new Page<>(request, response));
        bisDispatch.setStatus("0");
        Page<BisDispatch> page = bisDispatchService.findPage(bisDispatch);
        page.getList().forEach(bisDispatch1 -> {
            if ("1".equals(bisDispatch1.getOrderStatus())) {
                Calendar orderTime = Calendar.getInstance();
                orderTime.setTime(bisDispatch1.getOrderTime());
                orderTime.add(Calendar.MINUTE, overtime);
                if (Calendar.getInstance().compareTo(orderTime) > 0) {
                    bisDispatch1.setOrderStatus("5");//已过期
                }
            }
        });
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("bis:bisDispatch:view")
    @RequestMapping(value = "form")
    public String form(BisDispatch bisDispatch, Model model) {
        if (StringUtils.isBlank(bisDispatch.getId())) {
            bisDispatch.setStatus("1");
        }
        model.addAttribute("bisDispatch", bisDispatch);
        return "modules/bis/bisDispatchForm";
    }

    /**
     * 保存派单管理
     */
    @RequiresPermissions("bis:bisDispatch:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated BisDispatch bisDispatch) {
        bisDispatch.setOrderStatus("1");
        bisDispatch.setOrderTime(new Date());
        if (bisDispatch.getIsNewRecord()) {
            if (StringUtils.isNotBlank(bisDispatch.getAssignorId())) {
                bisDispatch.setDipatchType(1L);
            } else {
                bisDispatch.setDipatchType(2L);
            }
        }
        bisDispatchService.save(bisDispatch);
        return renderResult(Global.TRUE, text("保存派单管理成功！"));
    }

    /**
     * 保存派单管理
     */
    @RequiresPermissions("bis:bisDispatch:edit")
    @PostMapping(value = "getArea")
    @ResponseBody
    public String getArea(BisDispatch bisDispatch) {
        Area area = areaService.get(bisDispatch.getAreaId());
        return renderResult(Global.TRUE, "成功", area);
    }

    /**
     * 删除派单管理
     */
    @RequiresPermissions("bis:bisDispatch:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(BisDispatch bisDispatch) {
        bisDispatch.setStatus("1");
        bisDispatchService.updateStatus(bisDispatch);
        return renderResult(Global.TRUE, text("删除派单管理成功！"));
    }

    /**
     * 删除派单管理
     */
    @RequiresPermissions("bis:bisDispatch:edit")
    @RequestMapping(value = "release")
    @ResponseBody
    public String release(BisDispatch bisDispatch) {
        bisDispatch.setOrderStatus("1");
		bisDispatchService.updateOrderStatus(bisDispatch);
        return renderResult(Global.TRUE, text("重新发布成功！"));
    }

    @RequiresPermissions({"bis:bisDispatch:edit"})
    @RequestMapping({"exportData"})
    public void exportData(BisDispatch bisDispatch, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
        List<BisDispatch> list = this.bisDispatchService.findList(bisDispatch);
        String fileName = "派单数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        ExcelExport ee = new ExcelExport("派单数据", BisDispatch.class);
        Throwable var8 = null;

        try {
            ee.setDataList(list).write(response, fileName);
        } catch (Exception var17) {
            var8 = var17;
            throw var17;
        } finally {
            if (ee != null) {
                if (var8 != null) {
                    try {
                        ee.close();
                    } catch (Throwable var16) {
                        var8.addSuppressed(var16);
                    }
                } else {
                    ee.close();
                }
            }

        }

    }

}